package com.ct.controller.sysrole;

import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.sysapis.model.vo.SysApisVO;
import com.ct.application.sysrmenus.model.vo.SysMenusVO;
import com.ct.application.sysrole.model.vo.SysRolesMenuApisVO;
import com.ct.application.sysrole.model.vo.SysRolesVO;
import com.ct.application.sysrole.request.*;
import com.ct.application.sysrole.service.ISysRolesService;
import com.ct.application.sysrole.service.ISysUserRolesService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.ApiLog;
import com.ct.commons.annotation.HasAdmin;
import com.ct.commons.annotation.PermissionApi;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import com.ct.repository.commons.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  RBAC-角色管理
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@RestController
@PermissionApi(title = "角色管理模块",path="/api/v1/role",code = "Z1000",sorted = 14)
public class SysRolesController extends BaseController {

    @Autowired
    private ISysRolesService sysRolesService;
    @Autowired
    private ISysUserRolesService sysUserRolesService;

    /**
     * 角色列表--租户角色
     *
     * @param roleSearchRequest
     * @return
     */
    @PostMapping("/role/system/list")
    @PermissionApi(title = "角色管理模块",path="/api/v1/role",code = "Z1000",sorted = 1)
    public PageUtils<SysRolesVO> findSystemPage(@RequestBody SysRoleSearchRequest roleSearchRequest) {
        return sysRolesService.findSystemPage(roleSearchRequest);
    }

    /**
     * 角色列表--系统角色
     *
     * @param roleSearchRequest
     * @return
     */
    @PostMapping("/role/list")
    @PermissionApi(title = " 角色列表--系统角色",path="/api/v1/role/list",code = "Z1002",sorted = 2)
    public PageUtils<SysRolesVO> findPage(@RequestBody SysRoleSearchRequest roleSearchRequest) {
        return sysRolesService.findPage(roleSearchRequest);
    }


    /**
     * 保存后台角色
     * @param sysRoleSaveRequest
     * @return
     */
    @PostMapping("/role/save")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("保存后台角色")
    @PermissionApi(title = "保存后台角色",path="/api/v1/role/save",code = "Z1003",sorted = 3)
    public SysRolesVO saveSysRoles(@Validated @RequestBody SysRoleSaveRequest sysRoleSaveRequest){
        return sysRolesService.saveSysRoles(sysRoleSaveRequest);
    }

    /**
     * 修改后台角色
     * @param sysRoleSaveRequest
     * @return
     */
    @PostMapping("/role/update")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("修改后台角色")
    @PermissionApi(title = "修改后台角色",path="/api/v1/role/update",code = "Z1004",sorted = 4)
    public SysRolesVO updateSysRoles(@Validated @RequestBody SysRoleUpdateRequest sysRoleSaveRequest){
        return sysRolesService.updateSysRoles(sysRoleSaveRequest);
    }

    /**
     * 根据ID查询后台角色
     * @param id
     * @return
     */
    @PostMapping("/role/get/{id}")
    @PermissionApi(title = "根据ID查询后台角色",path="/api/v1/role/get/{id}",code = "Z1005",sorted = 5)
    public SysRolesVO getSysRoles(@PathVariable("id") Long id){
        return sysRolesService.selectById(id);
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PostMapping("/role/del/{id}")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("删除角色")
    @PermissionApi(title = "删除角色",path="/api/v1/role/del/{id}",code = "Z1006",sorted = 6)
    public Boolean delSysRoles(@PathVariable("id") Long id){
        return sysRolesService.deleteById(id);
    }

    /**
     * 根据字段修改状态
     * @param statusRequest
     * @return
     */
    @PostMapping("/role/update/status")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("修改状态")
    @PermissionApi(title = "修改状态",path="/api/v1/role/update/status",code = "Z1007",sorted = 7)
    public Boolean updateStatusSysRoles(@Validated @RequestBody StatusRequest statusRequest){
        return sysRolesService.updateStatusSysRoles(statusRequest);
    }

    /**
     * 批量通过
     * @param sysRoleBatchRequest
     * @return
     */
    @PostMapping("/role/batch/statuss")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("批量通过")
    @PermissionApi(title = "批量通过",path="/api/v1/role/batch/statuss",code = "Z1008",sorted = 8)
    public Boolean batchStatus(@Validated @RequestBody SysRoleBatchRequest sysRoleBatchRequest){
        return sysRolesService.batchStatus(sysRoleBatchRequest);
    }

    /**
     * 批量禁止
     * @param sysRoleBatchRequest
     * @return
     */
    @PostMapping("/role/batch/forbiddens")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("批量禁止")
    @PermissionApi(title = "批量禁止",path="/api/v1/role/batch/forbiddens",code = "Z1009",sorted = 9)
    public Boolean batchForbiddens(@Validated @RequestBody SysRoleBatchRequest sysRoleBatchRequest){
        return sysRolesService.batchForbiddens(sysRoleBatchRequest);
    }


    /**
     * 角色切换
     *
     * @param roleId
     * @return
     */
    @PostMapping("/role/change")
    @ApiLog("角色切换")
    @PermissionApi(title = "角色切换",path="/api/v1/role/change",code = "Z1010",sorted = 10)
    public SysRolesMenuApisVO changeRole(@RequestParam("roleId") Long roleId) {
        return sysRolesService.findRoleMenusPermission(roleId);
    }

    /**
     * 角色绑定菜单
     *
     * @param sysRoleBindMenuRequest
     * @return
     */
    @PostMapping("/role/bind/menus")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("角色绑定菜单")
    @PermissionApi(title = "角色绑定菜单",path="/api/v1/role/menus",code = "Z1011",sorted = 11)
    public Boolean bindMenus(@RequestBody SysRoleBindMenuRequest sysRoleBindMenuRequest) {
        return sysRolesService.bindMenus(sysRoleBindMenuRequest);
    }

    /**
     * 角色绑定权限
     *
     * @param sysRoleBindApisRequest
     * @return
     */
    @PostMapping("/role/bind/apis")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("角色绑定权限")
    @PermissionApi(title = "角色绑定权限",path="/api/v1/role/apis",code = "Z1012",sorted = 12)
    public Boolean bindApis(@RequestBody SysRoleBindApisRequest sysRoleBindApisRequest) {
        return sysRolesService.bindApis(sysRoleBindApisRequest);
    }


    /**
     * 根据角色id查询对应的菜单
     *
     * @param roleId
     * @return
     */
    @PostMapping("/role/menus")
    @PermissionApi(title = "根据角色id查询对应的菜单",path="/api/v1/role/menus",code = "Z1013",sorted = 13)
    public List<SysMenusVO> findRoleMenus(@RequestParam("roleId") Long roleId){
        return sysRolesService.findRoleMenus(roleId);
    }

    /**
     * 根据角色id查询对应的权限
     *
     * @param roleId
     * @return
     */
    @PostMapping("/role/apis")
    @PermissionApi(title = "根据角色id查询对应的权限",path="/api/v1/role/apis",code = "Z1014",sorted = 14)
    public List<SysApisVO> findRoleApis(@RequestParam("roleId") Long roleId) {
        return sysRolesService.findRoleApis(roleId);
    }

    /**
     * 根据角色id绑定对应的权限
     *
     * @param sysRoleBindRequest
     * @return
     */
    @PostMapping("/role/bind")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("绑定角色")
    @PermissionApi(title = "根据角色id绑定对应的权限",path="/api/v1/role/bind",code = "Z1015",sorted = 15)
    public Boolean saveBindUserRole(@RequestBody SysRoleBindRequest sysRoleBindRequest) {
        return sysUserRolesService.saveBindUserRole(sysRoleBindRequest);
    }

    /**
     * 根据角色id查询对应的权限
     *
     * @param sysRoleBindRequest
     * @return
     */
    @PostMapping("/role/cancle")
    @HasAdmin
    @AccessIpLimiter
    @ApiLog("取消角色绑定")
    @PermissionApi(title = "取消角色绑定",path="/api/v1/role/cancle",code = "Z1016",sorted = 16)
    public Boolean cancleBindUserRole(@RequestBody SysRoleBindRequest sysRoleBindRequest) {
        return sysUserRolesService.cancleBindUserRole(sysRoleBindRequest);
    }
}
